package cn.openea.perpartner.meily.web.controller;

import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.Article;
import cn.openea.perpartner.meily.web.entity.vo.ArticleSearchVo;
import cn.openea.perpartner.meily.web.entity.vo.ArticleVo;
import cn.openea.perpartner.meily.web.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author rains
 * @since 2023-06-19 14:51
 */
@Api(tags = "女性频道")
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {

    @Resource
    private IArticleService articleService;

    @ApiOperation("列表")
    @PostMapping("/page/list")
    public TableDataInfo list(@RequestBody ArticleSearchVo searchVo) {
        startPage();
        List<Article> list = articleService.selectList(searchVo);
        return getDataTable(list);
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Valid ArticleVo articleVo) {
        articleService.addArticle(articleVo);
        return AjaxResult.success();
    }

    @ApiOperation("修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Valid ArticleVo articleVo) {
        articleService.updateArticle(articleVo);
        return AjaxResult.success();
    }

    @ApiOperation("置顶文章")
    @PostMapping("/top/{id}")
    public AjaxResult top(@PathVariable Long id) {
        return toAjax(articleService.topArticle(id));
    }

    @ApiOperation("取消置顶文章")
    @PostMapping("/top/cancel/{id}")
    public AjaxResult cancelTop(@PathVariable Long id) {
        return toAjax(articleService.cancelTop(id));
    }

    @ApiOperation("上架文章")
    @PostMapping("/shelf/{id}")
    public AjaxResult shelfCourse(@PathVariable Long id) {
        return toAjax(articleService.shelfArticle(id));
    }

    @ApiOperation("下架文章")
    @PostMapping("/down/{id}")
    public AjaxResult downCourse(@PathVariable Long id) {
        return toAjax(articleService.downArticle(id));
    }


    @ApiOperation("删除")
    @PostMapping("/delete")
    public AjaxResult deleteById(@RequestBody List<Long> idList) {
        return success(articleService.removeByIds(idList));
    }

    @ApiOperation("获取详情")
    @GetMapping("/info/{id}")
    public AjaxResult getInfoById(@PathVariable String id) {
        if (StringUtils.isBlank(id)) {
            return AjaxResult.error("数据异常，编号为空");
        }
        return AjaxResult.success("获取详情成功！", articleService.getById(id));
    }

    @ApiOperation("列表-小程序")
    @PostMapping("/applet/list")
    public AjaxResult getList(@RequestBody ArticleSearchVo searchVo) {
        // 查询所有已置顶数据---不分页
        if("1".equals(searchVo.getIsTop())){
            List<Article> list = articleService.getList(searchVo);
            return AjaxResult.success("获取所有置顶文章成功！", list);
        } else {
            // 查询未置顶数据---分页
            startPage();
            List<Article> list = articleService.getList(searchVo);
            AjaxResult ajaxResult = AjaxResult.success("获取文章成功！", list);
            ajaxResult.put("total", list.size());
            return ajaxResult;
        }
    }
}
